(function($){
	$.fn.picToggle = function(options_events){
		var opts = $.extend({}, 
							{	
								/*options*/
								speed: 300,
								prebtn: {},
								nextbtn: {}
							},
							options_events);
							
		var picToggleNameSpace = {
			t: '',
			pressing: false
		};
		
		var moveLeft = function(obj){
			objLeft = obj.position().left;
			itemWidth = obj.children().outerWidth();
			if(obj.position().left < 0 && picToggleNameSpace.pressing){
				clearTimeout(picToggleNameSpace.t);
				picToggleNameSpace.t = setTimeout(function(){obj.css({left: objLeft+itemWidth/5}); moveLeft(obj);}, opts.speed);
			}
		};
		
		var moveRight = function(obj){
			var elementWidth = obj.parent().width();
			objLeft = obj.position().left;
			objWidth = obj.outerWidth();
			itemWidth = obj.children().outerWidth();
			if(elementWidth - objLeft < objWidth && picToggleNameSpace.pressing){
				clearTimeout(picToggleNameSpace.t);
				picToggleNameSpace.t = setTimeout(function(){obj.css({left: objLeft-itemWidth/5}); moveRight(obj);}, opts.speed);
			}
		};
		
		return $(this).each(function(){
			var element = $(this);
		
			var widthTotal = 0;
			var heightMax = 0;
			$.each(element.children(), function(){
				var onePic = $(this);
				widthTotal += onePic.outerWidth();
				heightMax = (onePic.outerHeight() > heightMax)? onePic.outerHeight() : heightMax;
			});
			
			element.css({
				overflow: 'hidden',
				height: heightMax,
				position: 'relative'
			});
			
			var picToggleWrap = $('<div id="picToggleWrap" />');
			picToggleWrap.css({
				width: widthTotal,
				height: heightMax,
				position: 'absolute',
				left: 0,
				top: 0,
				float: element.children().css('float')
			});
			
			element.children().wrapAll(picToggleWrap);
			
			opts.prebtn.mousedown(function(){
				picToggleNameSpace.pressing = true;
				moveLeft($('#picToggleWrap'));
			}).mouseup(function(){
				picToggleNameSpace.pressing = false;
			}).css({'line-height': heightMax+'px'});
			
			opts.nextbtn.mousedown(function(){
				picToggleNameSpace.pressing = true;
				moveRight($('#picToggleWrap'));
			}).mouseup(function(){
				picToggleNameSpace.pressing = false;
			}).css({'line-height': heightMax+'px'});
			
		});
	};
})(jQuery);